package com.ezcolour.ticket.service.impl;

import com.ezcolour.ticket.bean.Credence;
import com.ezcolour.ticket.dao.MemberDao;
import com.ezcolour.ticket.model.Member;
import com.ezcolour.ticket.service.MemberService;
import com.ezcolour.ticket.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class MemberServiceImpl extends BaseServiceImpl<Member> implements MemberService {

    @Autowired
    private MemberDao memberDao;

    public MemberDao getBaseRepository(){
        return memberDao;
    }

    @Override
    public boolean validate(Credence credence) {
        List<Member> members = memberDao.findOneByPhoneNumber(credence.getCredenceName());
        if(members != null && members.size() > 0){
            Member member = members.get(0);
            boolean verify = PasswordUtil.verify(member.getSalt(), credence.getCredenceCode(), member.getPassword());
            return verify;
        }else {
            return false;
        }
    }

    @Override
    public Member findByPhoneNumber(String phoneNumber) {
        List<Member> members = memberDao.findOneByPhoneNumber(phoneNumber);
        if(members != null && members.size() > 0){
            return members.get(0);
        }else {
            return null;
        }
    }
}
